import Crypt from "../../../utils/Crypt";
import CityModel from "../../../models/City";

const cityModel = new CityModel()

Page({
  data: {
    list: []
  },
  onLoad(options) {
  },

  onShow: function () {
    cityModel.list().then(response => {
      const { data } = response
      this.setData({ list: data })
    })
  },

  add() {
    wx.navigateTo({
      url: '/sub_service/pages/address-edit/index'
    })
  },

  deleteAddress(e) {
    wx.showModal({
      title: '确定要删除吗?',
      success: (res) => {
        if (res.confirm) {
          const item = e.currentTarget.dataset.item
          cityModel.remove({ id: item.id }).then(() => {
            const list = this.data.list.filter(x => x.id !== item.id)
            wx.showToast({ title: '删除成功' })
            this.setData({ list })
          })
        }
      }
    })
  },

  onEdit(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/sub_service/pages/address-edit/index?id=' + item.id
    })
  }
});
